package app;/**
 * @description:
 * @authoer: Max Woods
 * @create:2018/6/29 17:07
 **/


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import nimble.json.FastjsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;

import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.List;

/**
 * @auther: Max Woods
 * @create:2018/6/29 17:07
 **/
public class DataGen {

    private static final Logger logger = LoggerFactory.getLogger(DataGen.class);

    public static void main(String[] args) throws Exception {
        String filePath = "D:/华夏收藏拍品链接.xls";
        String jsonFile="D:/华夏收藏拍品链接.json";
        WebClient webClient = new WebClient();
        FileInputStream fis = new FileInputStream(filePath);
        final HSSFWorkbook workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);
        JSONArray ja=new JSONArray();
        GoodsData goodsData;
        //getData(webClient,"http://www.huayicang.com/hxgq/xpai/goods_18-0611-1-163.html");
        for (int i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i++) {
            String url = sheet.getRow(i).getCell(0).getStringCellValue();
            logger.info("url[{}]:{}", i, url);
            try {
                goodsData = getData(webClient, url);
                ja.add(JSON.toJSON(goodsData));
                logger.info("产品数据:{}", FastjsonUtils.toJSONString(goodsData));
            }
            catch (Exception e)
            {
                logger.error("提取数据异常:{}",url);
            }
        }
        workbook.close();
        fis.close();
        webClient.close();
        writeTextFile(jsonFile,ja);
    }

    public static GoodsData getData(WebClient webClient, String url) throws Exception {
        logger.info("提取拍品数据：{}", url);
        GoodsData goodsData=new GoodsData();
        HtmlPage htmlPage = null;
        htmlPage = webClient.getPage(url);
        HtmlDivision boxNode = (HtmlDivision) htmlPage.getByXPath("//html//body//div[@class='wrapper']//div[@class='main']//div[@id='xpai_single']//div[@id='xpai_single_left']//div[@class='box']").get(0);
        HtmlParagraph paragraph=(HtmlParagraph)boxNode.getByXPath("div[@class='box_01']//div[contains(@class,'box_01_right')]//p[@class='pu_price']").get(0);
        HtmlDivision nameNode = (HtmlDivision) boxNode.getByXPath("div[@class='tabtop']//div[@class='name']").get(0);
        String name = nameNode.getFirstChild().getTextContent();
        byte bytes[] = {(byte) 0xC2, (byte) 0xA0};
        String UTFSpace = new String(bytes, "utf-8");
        name = StringUtils.replaceAll(name, UTFSpace, StringUtils.EMPTY);
        name = StringUtils.replace(name, " ", StringUtils.EMPTY);
        name = StringUtils.replace(name, "\n", StringUtils.EMPTY);
        name = StringUtils.replace(name, "　", StringUtils.EMPTY);
        //
        HtmlDivision descNode = (HtmlDivision) boxNode.getByXPath("div[@id='box_content']//div[@class='miaoshu']").get(0);
        DomText descText = (DomText) descNode.getChildNodes().get(2);
        String desc = descText.getTextContent();
        desc = StringUtils.replace(desc, UTFSpace, StringUtils.EMPTY);
        desc = StringUtils.replace(desc, " ", StringUtils.EMPTY);
        desc = StringUtils.replace(desc, "\n", StringUtils.EMPTY);
        desc = StringUtils.replace(desc, "　", StringUtils.EMPTY);
        //
        goodsData.setName(name);
        goodsData.setDesc(desc);
        //
        List<DomNode> imagesNode = boxNode.getByXPath("div[@id='box_content']//div[@class='photo']//div[@id='xpg_pics']//div[@class='goods_pic3']");
        for (DomNode node : imagesNode) {
            Node n = node.getAttributes().getNamedItem("src");
            Node m=node.getChildNodes().get(0).getAttributes().getNamedItem("src");
            //logger.info("图片地址：{},高清图地址：{}", m.getNodeValue(),n.getNodeValue());
            goodsData.getImages().add(n.getNodeValue());
        }
        return goodsData;
    }

    private static void writeTextFile(String jsonFile,JSONArray ja) throws Exception
    {
        File writename = new File(jsonFile); // 相对路径，如果没有则要建立一个新的output。txt文件
        writename.createNewFile(); // 创建新文件
        BufferedWriter out = new BufferedWriter(new FileWriter(writename));
        out.write(JSON.toJSONString(ja,SerializerFeature.PrettyFormat)); // \r\n即为换行
        out.flush(); // 把缓存区内容压入文件
        out.close(); // 最后记得关闭文件
    }
}
